import com.fasterxml.jackson.databind.ObjectMapper;
import entity.Classes;
import mapper.CodeAnalysisMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

// 3. 改造后的导入逻辑
public class MyBatisImporter {
    private final SqlSessionFactory sqlSessionFactory;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public MyBatisImporter() {
        String resource = "mybatis-config.xml";
        try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("MyBatis配置初始化失败", e);
        }
    }

    public void importData() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            CodeAnalysisMapper mapper = session.getMapper(CodeAnalysisMapper.class);

            // 读取JSON数据
            Classes classData = readJsonData();

            // 插入主类
            mapper.insertClass(classData);
            String classId = classData.getId();

            // 插入字段（包含批处理）
            if (!classData.getFields().isEmpty()) {
                mapper.insertFields(classId, classData.getFields());
                // 处理字段注解
                processFieldAnnotations(mapper, classData.getFields());
            }

            // 处理方法
            processMethods(mapper, classId, classData.getFunctions());

            // 插入类级别注解
            if (!classData.getAnnotations().isEmpty()) {
                mapper.insertAnnotations("CLASS", classId, classData.getAnnotations());
            }

            // 其他关联数据
            if (!classData.getImports().isEmpty()) {
                mapper.insertImports(classId, classData.getImports());
            }
            if (!classData.getImplements().isEmpty()) {
                mapper.insertImplements(classId, classData.getImplements());
            }

            session.commit();
        }
    }

    private void processMethods(CodeAnalysisMapper mapper, int classId, List<Method> methods) {
        for (Method method : methods) {
            method.setClassId(classId);
            mapper.insertMethod(method);
            int methodId = method.getId();

            // 方法参数
            if (!method.getParameters().isEmpty()) {
                mapper.insertParameters(methodId, method.getParameters());
            }

            // 函数调用
            if (!method.getFunctionCalls().isEmpty()) {
                mapper.insertFunctionCalls(methodId, method.getFunctionCalls());
            }

            // 方法注解
            if (!method.getAnnotations().isEmpty()) {
                mapper.insertAnnotations("METHOD", methodId, method.getAnnotations());
            }
        }
    }

    private void processFieldAnnotations(CodeAnalysisMapper mapper, List<Field> fields) {
        // 假设fields已包含数据库生成的ID（需要开启字段级主键返回）
        for (Field field : fields) {
            if (!field.getAnnotations().isEmpty()) {
                mapper.insertAnnotations("FIELD", field.getId(), field.getAnnotations());
            }
        }
    }

    private Classes readJsonData() {
        try (InputStream is = getClass().getResourceAsStream("/webconfig.json")) {
            return objectMapper.readValue(is, Classes.class);
        } catch (IOException e) {
            throw new RuntimeException("JSON读取失败", e);
        }
    }

    public static void main(String[] args) {
        new MyBatisImporter().importData();
    }
}